﻿using BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Models;

namespace CSMF.Management.Page_User
{
    public partial class UserList : System.Web.UI.Page
    {
        private UserService userManager = new UserService();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["admin"] != null)
                {
                    BindDataForGridView(false);
                }
                else
                {
                    Response.Write("<script language='javascript'>parent.location.href='../login.aspx';</script>");
                }
            }
        }

        private void BindDataForGridView(bool isDeleted)
        {
            List<UserInfo> users = userManager.GetAllUsersForAdmin(isDeleted);
            BindData(users);
        }

        private void BindData(List<UserInfo> users)
        {
            this.AspNetPager1.RecordCount = users.Count;
            this.AspNetPager1.PageSize = 20;
            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = users;
            pds.AllowPaging = true;
            pds.PageSize = 20;
            pds.CurrentPageIndex = this.AspNetPager1.CurrentPageIndex - 1;
            gvUser.DataSource = pds;
            gvUser.DataBind();
        }

        protected void gvUser_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            LinkButton deleteButton = this.gvUser.Rows[e.RowIndex].FindControl("lbDelete") as LinkButton;
            int id = Convert.ToInt32(deleteButton.CommandArgument);
            userManager.DeleteUserById(id);

            BindDataForGridView(false);
        }

        protected void gvUser_Restore(object sender, EventArgs e)
        {
            LinkButton lbtn = (LinkButton)sender;
            int id = int.Parse(lbtn.CommandArgument);
            userManager.RestoreUserById(id);

            BindDataForGridView(false);
        }

        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            BindDataForGridView(false);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            List<UserInfo> users = null;
            string searchString = txtUserName.Text.Trim();
            if (string.IsNullOrEmpty(searchString) == false)
            {
                users = new List<UserInfo>();
                var user = userManager.GetUserByName(searchString);
                if (user != null)
                {
                    users.Add(user);
                }
            }
            else
            {
                users = userManager.GetAllUsersForAdmin(false);
            }

            BindData(users);
        }
    }
}